﻿using System;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace WebInfoExtractor
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private async void btnFetch_Click(object sender, EventArgs e)
        {
            string url = txtUrl.Text.Trim();
            if (string.IsNullOrEmpty(url))
            {
                MessageBox.Show("请输入有效的URL");
                return;
            }

            try
            {
                // 添加User-Agent标头，避免被某些网站拒绝
                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0");
                    string html = await client.GetStringAsync(url);

                    // 提取手机号码
                    var phoneMatches = Regex.Matches(html, @"1[3-9]\d{9}");
                    lstPhones.Items.Clear();
                    foreach (Match match in phoneMatches)
                    {
                        if (!lstPhones.Items.Contains(match.Value))
                            lstPhones.Items.Add(match.Value);
                    }

                    // 提取邮箱地址
                    var emailMatches = Regex.Matches(html,
                        @"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}");
                    lstEmails.Items.Clear();
                    foreach (Match match in emailMatches)
                    {
                        if (!lstEmails.Items.Contains(match.Value))
                            lstEmails.Items.Add(match.Value);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"错误: {ex.Message}");
            }
        }
    }
}